package com.example.music;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;


import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import androidx.room.Room;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.music.databinding.ActivityMainBinding;
import com.example.music.databinding.ChildrenMusicBinding;
import com.example.music.databinding.ChildrenToLoginBinding;
import com.example.music.dialog.BottomDialog;
import com.example.music.entity.DataMusic;
import com.example.music.room.AppDatabase;
import com.example.music.room.User;
import com.example.music.service.LocalService;
import com.example.music.service.RemoteService;
import com.example.music.dialog.CustomDialog;
import com.zackratos.ultimatebarx.library.UltimateBarX;

import java.util.List;

import site.gemus.openingstartanimation.OpeningStartAnimation;
import site.gemus.openingstartanimation.RotationDrawStrategy;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private NavController navController;
    private App application;
    private ChildrenToLoginBinding childrenToLoginBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupWithNavController(binding.navView, navController);
        Intent localService = new Intent(this, LocalService.class);
        this.startService(localService);
        Intent remoteService = new Intent(this, RemoteService.class);
        this.startService(remoteService);
        application = (App) getApplication();
        ChildrenMusicBinding musicBinding = ChildrenMusicBinding.inflate(LayoutInflater.from(this));
        musicBinding.getRoot().setVisibility(View.GONE);
        application.addDataMusics((DataMusic) null, new App.MusicEvent() {
            @Override
            public void onCompletion(String lyric) {

            }

            @Override
            public void onCurrent(DataMusic dataMusic) {
                musicBinding.getRoot().setVisibility(View.VISIBLE);
                musicBinding.tvName.setText(dataMusic.getName()+" - "+dataMusic.getAuthor());
                Glide.with(MainActivity.this)
                        .load(dataMusic.getImg_url())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(5)))
                        .into(musicBinding.ivLogoPlay);
            }

            @Override
            public void onStateChange(Boolean bool) {
                if(bool){
                    musicBinding.tvStartStop.setImageResource(R.mipmap.stop);
                }else {
                    musicBinding.tvStartStop.setImageResource(R.mipmap.start);
                }
            }

            @Override
            public void onNull() {
                musicBinding.getRoot().setVisibility(View.GONE);
            }

            @Override
            public void onData(List<DataMusic> dataMusics, int i) {

            }
        });
        musicBinding.tvStartStop.setOnClickListener(v -> application.pauseStart());
        musicBinding.ivMusicList.setOnClickListener(v -> {
            BottomDialog bottomDialog = new BottomDialog();
            bottomDialog.showCustomizeDialog(MainActivity.this, (App) getApplication());
        });
        musicBinding.getRoot().setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("musics",null);
            navController.navigate(R.id.navigation_play,bundle);
        });
        childrenToLoginBinding = ChildrenToLoginBinding.inflate(LayoutInflater.from(this));
        childrenToLoginBinding.getRoot().setOnClickListener(v -> navController.navigate(R.id.navigation_personal));
        binding.include.addView(childrenToLoginBinding.getRoot());
        binding.include.addView(musicBinding.getRoot());
        OpeningStartAnimation openingStartAnimation = new OpeningStartAnimation.Builder(this)
                .setDrawStategy(new RotationDrawStrategy())
                .setAppName("动感音乐")
                .setColorOfAppName(Color.parseColor("#0066ff"))
                .setAppStatement("移动互联网应用技术")
                .setColorOfAppStatement(Color.parseColor("#0066ff"))
                .setAnimationFinishTime(2000)
                .create();
        openingStartAnimation.show(this);
        AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = db.userDao().findByOnline(true);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(user==null){
                            childrenToLoginBinding.getRoot().setVisibility(View.VISIBLE);
                        }else {
                            childrenToLoginBinding.getRoot().setVisibility(View.GONE);
                        }
                    }
                });
            }
        }).start();
    }

    public void visibleAll(){
        binding.include.setVisibility(View.VISIBLE);
        binding.navView.setVisibility(View.VISIBLE);
    }
    public void visible(int tag){
       childrenToLoginBinding.getRoot().setVisibility(tag);
    }
    public void goneAll(){
        binding.include.setVisibility(View.GONE);
        binding.navView.setVisibility(View.GONE);
    }
    public void gone(){
        binding.include.setVisibility(View.GONE);
    }

    @SuppressLint("RestrictedApi")
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(navController.getCurrentDestination() == null){
            return super.onKeyDown(keyCode, event);
        }
        if(navController.getCurrentDestination().getLabel() == null){
            return super.onKeyDown(keyCode, event);
        }
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN
                && navController.getCurrentDestination().getLabel().toString().equalsIgnoreCase(getString(R.string.title_recommend))){
            CustomDialog.Builder builder = new CustomDialog.Builder(MainActivity.this);
            builder.setTitle("温馨提示");
            builder.setMessage("确认退出动感音乐");
            builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                    System.exit(0);
                }
            });
            builder.setNegativeButton("再听一会", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

}